package tq.com.tqteacher.adapt;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.security.PublicKey;
import java.util.ArrayList;

import tq.com.tqteacher.R;
import tq.com.tqteacher.activity.TQPdfReaderActivity;
import tq.com.tqteacher.model.TQDirectoryModel;
import tq.com.tqteacher.model.TQFileInfo;
import tq.com.tqteacher.view.TQShowAllLinearLayoutManager;

public class TQDirectoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private ArrayList<TQDirectoryModel> data = new ArrayList<>();

    public TQDirectoryAdapter(Context pContext){
        mContext = pContext;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup pViewGroup, int pI) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.vh_directory_layout, pViewGroup, false);
        return new DirectoryByDateViewHolder(v);
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder pViewHolder, int pI) {
        TQDirectoryModel lDirectoryModel = data.get(pI);
        if(pViewHolder instanceof DirectoryByDateViewHolder){
            ((DirectoryByDateViewHolder)pViewHolder).bindData(lDirectoryModel);
        }
    }

    public void appData(ArrayList<TQDirectoryModel> pData){
        if(pData == null) return;

        data.addAll(pData);
        notifyDataSetChanged();
    }

    public void clear(){
        data.clear();
        notifyDataSetChanged();
    }

    public class DirectoryByDateViewHolder extends RecyclerView.ViewHolder{
        private RecyclerView mRecyclerView;
        private TQDirectoryDetailAdapter mDetailAdapter;
        private TextView day;

        public DirectoryByDateViewHolder(View pView){
            super(pView);
            mRecyclerView = pView.findViewById(R.id.recyclerView);

            if(mRecyclerView != null){
                mRecyclerView.setLayoutManager(new TQShowAllLinearLayoutManager(mRecyclerView, LinearLayoutManager.VERTICAL, false));

                mRecyclerView.setNestedScrollingEnabled(false);
                if(mDetailAdapter == null){
                    mDetailAdapter = new TQDirectoryDetailAdapter(mContext);
                    mRecyclerView.setAdapter(mDetailAdapter);
                    mDetailAdapter.setFileClickListener(mFileClickListener);
                }
            }

            day = pView.findViewById(R.id.date);
        }

        public void bindData(TQDirectoryModel pDirectoryModel){
            if(pDirectoryModel ==  null) return;

            day.setText(pDirectoryModel.getDate());
            mDetailAdapter.clear();
            mDetailAdapter.appendData(pDirectoryModel.getDirectoryItems());
        }
    }

    private TQDirectoryDetailAdapter.IFileClickListener mFileClickListener = new TQDirectoryDetailAdapter.IFileClickListener() {
        @Override
        public void itemClick(TQFileInfo file) {
            Log.d(TAG, "itemClick: ");
            if(mIFileClickListener != null) mIFileClickListener.itemClick(file);
        }
    };

    private TQDirectoryDetailAdapter.IFileClickListener mIFileClickListener;
    public void setFileClickListener(TQDirectoryDetailAdapter.IFileClickListener pFileClickListener){
        mIFileClickListener = pFileClickListener;
    }
    static protected String TAG = "TQDirectoryAdapter";
}
